//
//  UIViewController+Alert.m
//  habit
//
//  Created by huck on 2016/11/16.
//  Copyright © 2016年 custom. All rights reserved.
//

#import "UIViewController+Alert.h"
#import <objc/runtime.h>
#import "UIColor+RGB.h"
#import "AppColor.h"

static const void *blockKey = &blockKey;

@implementation UIViewController (Alert)
@dynamic alarmViewClickBlock;

-(void)showAlertWithTitle:(NSString *)title message:(NSString *)message cancelBottonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitles clickBlock:(void (^)(NSInteger buttonIndex))clickBlock
{
    self.alarmViewClickBlock = clickBlock;
    if([[[UIDevice currentDevice] systemVersion] floatValue]<10.0){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil];
        [alertView show];
    }
    else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        __weak UIViewController *weakself = self;
        NSInteger index = 0;
        
        if(nil != cancelButtonTitle && ![@"" isEqualToString: cancelButtonTitle]){
            UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
                [weakself handleAlertBlock:index];
            }];
            [alertController addAction:actionCancel];
            index++;
        }
        
        if(nil != otherButtonTitles && ![@"" isEqualToString: otherButtonTitles]){
            UIAlertAction *actionOK = [UIAlertAction actionWithTitle:otherButtonTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                [weakself handleAlertBlock:index];
            }];
            [alertController addAction:actionOK];
        }
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

-(void)showActionSheetWithTitle:(NSString *)title message:(NSString *)message clickBlock:(void (^)(NSInteger buttonIndex))clickBlock cancelBottonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitle,...
{
    self.alarmViewClickBlock = clickBlock;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue]<8.0) {
        UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:nil otherButtonTitles:otherButtonTitle, nil];
        menu.tintColor = [UIColor redColor];
        menu.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
        [menu showInView:self.view];
    }
    else {
        __weak UIViewController *weakself = self;
        NSInteger index = 0;
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
        
        if(nil != cancelButtonTitle && ![@"" isEqualToString: cancelButtonTitle]){
            UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
                [weakself handleAlertBlock:index];
            }];
            [alertController addAction:actionCancel];
            index++;
        }
        
        va_list list;
        va_start(list, otherButtonTitle);
        
        if(nil != otherButtonTitle && ![@"" isEqualToString: otherButtonTitle]){
            UIAlertAction *actionOK = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                [weakself handleAlertBlock:index];
            }];
            [alertController addAction:actionOK];
            index++;
        }
        NSString* param;
        while ((param = va_arg( list, NSString *)))
        {
            if(nil != param && ![@"" isEqualToString: param]){
                UIAlertAction *action = [UIAlertAction actionWithTitle:param style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                    [weakself handleAlertBlock:index];
                }];
                [alertController addAction:action];
                index++;
            }
        }
        va_end(list);
//        [alertController.view setTintColor:[UIColor colorWithString:COLOR_0FD07C]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

#pragma mark uiActionsheetdelegate
//- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
//{
//    for (UIView *subViwe in actionSheet.subviews) {
//        if ([subViwe isKindOfClass:[UIButton class]]) {
//            UIButton *button = (UIButton*)subViwe;
//            [button setTitleColor:[UIColor colorWithString:COLOR_0FD07C]forState:UIControlStateNormal];
//        }
//    }
//}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self handleAlertBlock:buttonIndex];
}

#pragma mark uialertviewdelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self handleAlertBlock:buttonIndex];
}

//回调处理
-(void)handleAlertBlock:(NSInteger)buttonIndex
{
    if(self.alarmViewClickBlock){
        self.alarmViewClickBlock(buttonIndex);
    }
    self.alarmViewClickBlock  = nil;
}

-(XGAlarmViewClickBlock)alarmViewClickBlock
{
    return objc_getAssociatedObject(self, blockKey);
}

-(void)setAlarmViewClickBlock:(XGAlarmViewClickBlock)alarmViewClickBlock
{
    objc_setAssociatedObject(self, blockKey, alarmViewClickBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
